﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HF.Architecture.Data;
using HF.Framework;
using HF.Framework.SqlDataContract;
using HF.Framework.DataClientDBAgent;
using HF.Permission.Data;
using HF.WorkFlow.Const;

public partial class LinkManMgr : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            TreeNode tmpNode = new TreeNode();
            tmpNode.Value = "#1";
            tmpNode.Text = "个人通讯录";
            tmpNode.ImageUrl = @"images\tree_27.gif";
            tvArch.Nodes.Add(tmpNode);

            TreeNode tmpNode1 = new TreeNode();
            tmpNode1.Value = "#2";
            tmpNode1.Text = "公共通讯录";
            tmpNode1.ImageUrl = @"images\tree_27.gif";
            tvArch.Nodes.Add(tmpNode1);

            LoadArchitecture(WorkFlowConst.Architecture_Department, tvArch.Nodes);
            tvArch.ExpandDepth = 1;
           
        }
    }
    public void BuildNoRecords(GridView gridView, DataTable dt)
    {
        try
        {
            if (dt.Rows.Count == 0)
            {
                dt.Rows.Add(dt.NewRow());
                gridView.DataSource = dt;
                gridView.DataBind();
                int columnCount = gridView.Rows[0].Cells.Count;
                gridView.Rows[0].Cells.Clear();
                gridView.Rows[0].Cells.Add(new TableCell());
                gridView.Rows[0].Cells[0].ColumnSpan = columnCount;
                gridView.Rows[0].Cells[0].Text = "没有数据.";
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    /// <summary>
    /// 递归装载全部组织结构
    /// </summary>
    /// <param name="key"></param>
    /// <param name="startNodes"></param>
    private void LoadArchitecture(string key, TreeNodeCollection startNodes)
    {
        try
        {
            if (key == null || key == "") key = "0";
            DataTable table = ArchitectureData.GetInstance().GetChildArchitectures(key);
            foreach (DataRow row in table.Rows)
            {

                TreeNode tmpNode = new TreeNode();
                tmpNode.Value = row["ArchitectureId"].ToString();
                tmpNode.Text = row["Caption"].ToString();
                if (row["TypeName"].ToString() == WorkFlowConst.Architecture_Department)
                {
                     tmpNode.ImageUrl = @"images\tree_27.gif";

                }
                else
                    if (row["TypeName"].ToString() == WorkFlowConst.Architecture_Duty)
                    {
                          tmpNode.ImageUrl = @"images\tree_27.gif";
                    }
  
                startNodes.Add(tmpNode);
                LoadArchitecture(tmpNode.Value, tmpNode.ChildNodes);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void tvArch_SelectedNodeChanged(object sender, EventArgs e)
    {
        if (tvArch.SelectedNode != null)
        {
            string archId = tvArch.SelectedNode.Value;
            if (archId == "#1")//个人通讯录
            {
                string sql = "select * from OA_Address where AddType='1' and Account=@Account";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sql;
                sqlItem.AppendParameter("@Account", this.User.Identity.Name);
                ClientDBAgent agent = new ClientDBAgent();
                DataTable userTb = agent.ExecuteDataTable(sqlItem);
                gvAddresslist.DataSource = userTb;
                gvAddresslist.DataBind();
                gvAddresslist.Visible = true;
                gvUserlist.Visible = false;
                BuildNoRecords(gvAddresslist, userTb);
            }
            else
                if (archId == "#2")//公共通讯录
                {
                    string sql = "select * from OA_Address where AddType='2'";
                    SqlDataItem sqlItem = new SqlDataItem();
                    sqlItem.CommandText = sql;
                    ClientDBAgent agent = new ClientDBAgent();
                    DataTable userTb = agent.ExecuteDataTable(sqlItem) ;
                    gvAddresslist.DataSource = userTb;
                    gvAddresslist.DataBind();
                    gvAddresslist.Visible = true;
                    gvUserlist.Visible = false;
                    BuildNoRecords(gvAddresslist, userTb);
                }
                else//组织结构
                {
                    string sql = "select * from HF_ArchDutyUserView where ArchitectureId=@ArchitectureId";
                    SqlDataItem sqlItem = new SqlDataItem();
                    sqlItem.CommandText = sql;
                    sqlItem.AppendParameter("@ArchitectureId", archId);
                    ClientDBAgent agent = new ClientDBAgent();
                    DataTable userTb = agent.ExecuteDataTable(sqlItem);
                    gvUserlist.DataSource = userTb;
                    gvUserlist.DataBind();
                    gvUserlist.Visible = true;
                    gvAddresslist.Visible = false;
                    BuildNoRecords(gvUserlist, userTb);
                }
        }
    }
    protected void btnPaddress_Click(object sender, EventArgs e)
    {
        Response.Write("<script>window.open('LinkManEdit.aspx?AddType=1&State=新建','_blank')</script>");
    }
    protected void btnCaddress_Click(object sender, EventArgs e)
    {
        Response.Write("<script>window.open('CompanyLinkManEdit.aspx?AddType=2&State=新建','_blank')</script>");
    }
}
